<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>formatter.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_debug_formatter.js.html">formatter.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition of various formatters for logging. Please minimize
<a name="line17"></a> * dependencies this file has on other closure classes as any dependency it
<a name="line18"></a> * takes won&#39;t be able to use the logging infrastructure.
<a name="line19"></a> *
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.debug.Formatter&#39;);
<a name="line23"></a>goog.provide(&#39;goog.debug.HtmlFormatter&#39;);
<a name="line24"></a>goog.provide(&#39;goog.debug.TextFormatter&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.debug.RelativeTimeProvider&#39;);
<a name="line27"></a>goog.require(&#39;goog.string&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Base class for Formatters. A Formatter is used to format a LogRecord into
<a name="line33"></a> * something that can be displayed to the user.
<a name="line34"></a> *
<a name="line35"></a> * @param {string=} opt_prefix The prefix to place before text records.
<a name="line36"></a> * @constructor
<a name="line37"></a> */
<a name="line38"></a>goog.debug.Formatter = function(opt_prefix) {
<a name="line39"></a>  this.prefix_ = opt_prefix || &#39;&#39;;
<a name="line40"></a>
<a name="line41"></a>  /**
<a name="line42"></a>   * A provider that returns the relative start time.
<a name="line43"></a>   * @type {goog.debug.RelativeTimeProvider}
<a name="line44"></a>   * @private
<a name="line45"></a>   */
<a name="line46"></a>  this.startTimeProvider_ =
<a name="line47"></a>      goog.debug.RelativeTimeProvider.getDefaultInstance();
<a name="line48"></a>};
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>/**
<a name="line52"></a> * Whether to show absolute time in the DebugWindow.
<a name="line53"></a> * @type {boolean}
<a name="line54"></a> */
<a name="line55"></a>goog.debug.Formatter.prototype.showAbsoluteTime = true;
<a name="line56"></a>
<a name="line57"></a>
<a name="line58"></a>/**
<a name="line59"></a> * Whether to show relative time in the DebugWindow.
<a name="line60"></a> * @type {boolean}
<a name="line61"></a> */
<a name="line62"></a>goog.debug.Formatter.prototype.showRelativeTime = true;
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * Whether to show the logger name in the DebugWindow.
<a name="line67"></a> * @type {boolean}
<a name="line68"></a> */
<a name="line69"></a>goog.debug.Formatter.prototype.showLoggerName = true;
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Whether to show the logger exception text.
<a name="line74"></a> * @type {boolean}
<a name="line75"></a> */
<a name="line76"></a>goog.debug.Formatter.prototype.showExceptionText = false;
<a name="line77"></a>
<a name="line78"></a>
<a name="line79"></a>/**
<a name="line80"></a> * Whether to show the severity level.
<a name="line81"></a> * @type {boolean}
<a name="line82"></a> */
<a name="line83"></a>goog.debug.Formatter.prototype.showSeverityLevel = false;
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>/**
<a name="line87"></a> * Formats a record.
<a name="line88"></a> * @param {goog.debug.LogRecord} logRecord the logRecord to format.
<a name="line89"></a> * @return {string} The formatted string.
<a name="line90"></a> */
<a name="line91"></a>goog.debug.Formatter.prototype.formatRecord = goog.abstractMethod;
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * Sets the start time provider. By default, this is the default instance
<a name="line96"></a> * but can be changed.
<a name="line97"></a> * @param {goog.debug.RelativeTimeProvider} provider The provider to use.
<a name="line98"></a> */
<a name="line99"></a>goog.debug.Formatter.prototype.setStartTimeProvider = function(provider) {
<a name="line100"></a>  this.startTimeProvider_ = provider;
<a name="line101"></a>};
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Returns the start time provider. By default, this is the default instance
<a name="line106"></a> * but can be changed.
<a name="line107"></a> * @return {goog.debug.RelativeTimeProvider} The start time provider.
<a name="line108"></a> */
<a name="line109"></a>goog.debug.Formatter.prototype.getStartTimeProvider = function() {
<a name="line110"></a>  return this.startTimeProvider_;
<a name="line111"></a>};
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Resets the start relative time.
<a name="line116"></a> */
<a name="line117"></a>goog.debug.Formatter.prototype.resetRelativeTimeStart = function() {
<a name="line118"></a>  this.startTimeProvider_.reset();
<a name="line119"></a>};
<a name="line120"></a>
<a name="line121"></a>
<a name="line122"></a>/**
<a name="line123"></a> * Returns a string for the time/date of the LogRecord.
<a name="line124"></a> * @param {goog.debug.LogRecord} logRecord The record to get a time stamp for.
<a name="line125"></a> * @return {string} A string representation of the time/date of the LogRecord.
<a name="line126"></a> * @private
<a name="line127"></a> */
<a name="line128"></a>goog.debug.Formatter.getDateTimeStamp_ = function(logRecord) {
<a name="line129"></a>  var time = new Date(logRecord.getMillis());
<a name="line130"></a>  return goog.debug.Formatter.getTwoDigitString_((time.getFullYear() - 2000)) +
<a name="line131"></a>         goog.debug.Formatter.getTwoDigitString_((time.getMonth() + 1)) +
<a name="line132"></a>         goog.debug.Formatter.getTwoDigitString_(time.getDate()) + &#39; &#39; +
<a name="line133"></a>         goog.debug.Formatter.getTwoDigitString_(time.getHours()) + &#39;:&#39; +
<a name="line134"></a>         goog.debug.Formatter.getTwoDigitString_(time.getMinutes()) + &#39;:&#39; +
<a name="line135"></a>         goog.debug.Formatter.getTwoDigitString_(time.getSeconds()) + &#39;.&#39; +
<a name="line136"></a>         goog.debug.Formatter.getTwoDigitString_(
<a name="line137"></a>             Math.floor(time.getMilliseconds() / 10));
<a name="line138"></a>};
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * Returns the number as a two-digit string, meaning it prepends a 0 if the
<a name="line143"></a> * number if less than 10.
<a name="line144"></a> * @param {number} n The number to format.
<a name="line145"></a> * @return {string} A two-digit string representation of {@code n}.
<a name="line146"></a> * @private
<a name="line147"></a> */
<a name="line148"></a>goog.debug.Formatter.getTwoDigitString_ = function(n) {
<a name="line149"></a>  if (n &lt; 10) {
<a name="line150"></a>    return &#39;0&#39; + n;
<a name="line151"></a>  }
<a name="line152"></a>  return String(n);
<a name="line153"></a>};
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * Returns a string for the number of seconds relative to the start time.
<a name="line158"></a> * Prepads with spaces so that anything less than 1000 seconds takes up the
<a name="line159"></a> * same number of characters for better formatting.
<a name="line160"></a> * @param {goog.debug.LogRecord} logRecord The log to compare time to.
<a name="line161"></a> * @param {number} relativeTimeStart The start time to compare to.
<a name="line162"></a> * @return {string} The number of seconds of the LogRecord relative to the
<a name="line163"></a> *     start time.
<a name="line164"></a> * @private
<a name="line165"></a> */
<a name="line166"></a>goog.debug.Formatter.getRelativeTime_ = function(logRecord,
<a name="line167"></a>                                                 relativeTimeStart) {
<a name="line168"></a>  var ms = logRecord.getMillis() - relativeTimeStart;
<a name="line169"></a>  var sec = ms / 1000;
<a name="line170"></a>  var str = sec.toFixed(3);
<a name="line171"></a>
<a name="line172"></a>  var spacesToPrepend = 0;
<a name="line173"></a>  if (sec &lt; 1) {
<a name="line174"></a>    spacesToPrepend = 2;
<a name="line175"></a>  } else {
<a name="line176"></a>    while (sec &lt; 100) {
<a name="line177"></a>      spacesToPrepend++;
<a name="line178"></a>      sec *= 10;
<a name="line179"></a>    }
<a name="line180"></a>  }
<a name="line181"></a>  while (spacesToPrepend-- &gt; 0) {
<a name="line182"></a>    str = &#39; &#39; + str;
<a name="line183"></a>  }
<a name="line184"></a>  return str;
<a name="line185"></a>};
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>
<a name="line189"></a>/**
<a name="line190"></a> * Formatter that returns formatted html. See formatRecord for the classes
<a name="line191"></a> * it uses for various types of formatted output.
<a name="line192"></a> *
<a name="line193"></a> * @param {string=} opt_prefix The prefix to place before text records.
<a name="line194"></a> * @constructor
<a name="line195"></a> * @extends {goog.debug.Formatter}
<a name="line196"></a> */
<a name="line197"></a>goog.debug.HtmlFormatter = function(opt_prefix) {
<a name="line198"></a>  goog.debug.Formatter.call(this, opt_prefix);
<a name="line199"></a>};
<a name="line200"></a>goog.inherits(goog.debug.HtmlFormatter, goog.debug.Formatter);
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/**
<a name="line204"></a> * Whether to show the logger exception text
<a name="line205"></a> * @type {boolean}
<a name="line206"></a> * @override
<a name="line207"></a> */
<a name="line208"></a>goog.debug.HtmlFormatter.prototype.showExceptionText = true;
<a name="line209"></a>
<a name="line210"></a>
<a name="line211"></a>/**
<a name="line212"></a> * Formats a record
<a name="line213"></a> * @param {goog.debug.LogRecord} logRecord the logRecord to format.
<a name="line214"></a> * @return {string} The formatted string as html.
<a name="line215"></a> * @override
<a name="line216"></a> */
<a name="line217"></a>goog.debug.HtmlFormatter.prototype.formatRecord = function(logRecord) {
<a name="line218"></a>  var className;
<a name="line219"></a>  switch (logRecord.getLevel().value) {
<a name="line220"></a>    case goog.debug.Logger.Level.SHOUT.value:
<a name="line221"></a>      className = &#39;dbg-sh&#39;;
<a name="line222"></a>      break;
<a name="line223"></a>    case goog.debug.Logger.Level.SEVERE.value:
<a name="line224"></a>      className = &#39;dbg-sev&#39;;
<a name="line225"></a>      break;
<a name="line226"></a>    case goog.debug.Logger.Level.WARNING.value:
<a name="line227"></a>      className = &#39;dbg-w&#39;;
<a name="line228"></a>      break;
<a name="line229"></a>    case goog.debug.Logger.Level.INFO.value:
<a name="line230"></a>      className = &#39;dbg-i&#39;;
<a name="line231"></a>      break;
<a name="line232"></a>    case goog.debug.Logger.Level.FINE.value:
<a name="line233"></a>    default:
<a name="line234"></a>      className = &#39;dbg-f&#39;;
<a name="line235"></a>      break;
<a name="line236"></a>  }
<a name="line237"></a>
<a name="line238"></a>  // Build message html
<a name="line239"></a>  var sb = [];
<a name="line240"></a>  sb.push(this.prefix_, &#39; &#39;);
<a name="line241"></a>  if (this.showAbsoluteTime) {
<a name="line242"></a>    sb.push(&#39;[&#39;, goog.debug.Formatter.getDateTimeStamp_(logRecord), &#39;] &#39;);
<a name="line243"></a>  }
<a name="line244"></a>  if (this.showRelativeTime) {
<a name="line245"></a>    sb.push(&#39;[&#39;,
<a name="line246"></a>        goog.string.whitespaceEscape(
<a name="line247"></a>            goog.debug.Formatter.getRelativeTime_(logRecord,
<a name="line248"></a>                this.startTimeProvider_.get())),
<a name="line249"></a>        &#39;s] &#39;);
<a name="line250"></a>  }
<a name="line251"></a>
<a name="line252"></a>  if (this.showLoggerName) {
<a name="line253"></a>    sb.push(&#39;[&#39;, goog.string.htmlEscape(logRecord.getLoggerName()), &#39;] &#39;);
<a name="line254"></a>  }
<a name="line255"></a>  if (this.showSeverityLevel) {
<a name="line256"></a>    sb.push(&#39;[&#39;, goog.string.htmlEscape(logRecord.getLevel().name), &#39;] &#39;);
<a name="line257"></a>  }
<a name="line258"></a>  sb.push(&#39;&lt;span class=&quot;&#39;, className, &#39;&quot;&gt;&#39;,
<a name="line259"></a>      goog.string.newLineToBr(goog.string.whitespaceEscape(
<a name="line260"></a>          goog.string.htmlEscape(logRecord.getMessage()))));
<a name="line261"></a>
<a name="line262"></a>  if (this.showExceptionText &amp;&amp; logRecord.getException()) {
<a name="line263"></a>    sb.push(&#39;&lt;br&gt;&#39;,
<a name="line264"></a>        goog.string.newLineToBr(goog.string.whitespaceEscape(
<a name="line265"></a>            logRecord.getExceptionText() || &#39;&#39;)));
<a name="line266"></a>  }
<a name="line267"></a>  sb.push(&#39;&lt;/span&gt;&lt;br&gt;&#39;);
<a name="line268"></a>
<a name="line269"></a>  // If the logger is enabled, open window and write html message to log
<a name="line270"></a>  // otherwise save it
<a name="line271"></a>  return sb.join(&#39;&#39;);
<a name="line272"></a>};
<a name="line273"></a>
<a name="line274"></a>
<a name="line275"></a>
<a name="line276"></a>/**
<a name="line277"></a> * Formatter that returns formatted plain text
<a name="line278"></a> *
<a name="line279"></a> * @param {string=} opt_prefix The prefix to place before text records.
<a name="line280"></a> * @constructor
<a name="line281"></a> * @extends {goog.debug.Formatter}
<a name="line282"></a> */
<a name="line283"></a>goog.debug.TextFormatter = function(opt_prefix) {
<a name="line284"></a>  goog.debug.Formatter.call(this, opt_prefix);
<a name="line285"></a>};
<a name="line286"></a>goog.inherits(goog.debug.TextFormatter, goog.debug.Formatter);
<a name="line287"></a>
<a name="line288"></a>
<a name="line289"></a>/**
<a name="line290"></a> * Formats a record as text
<a name="line291"></a> * @param {goog.debug.LogRecord} logRecord the logRecord to format.
<a name="line292"></a> * @return {string} The formatted string.
<a name="line293"></a> * @override
<a name="line294"></a> */
<a name="line295"></a>goog.debug.TextFormatter.prototype.formatRecord = function(logRecord) {
<a name="line296"></a>  // Build message html
<a name="line297"></a>  var sb = [];
<a name="line298"></a>  sb.push(this.prefix_, &#39; &#39;);
<a name="line299"></a>  if (this.showAbsoluteTime) {
<a name="line300"></a>    sb.push(&#39;[&#39;, goog.debug.Formatter.getDateTimeStamp_(logRecord), &#39;] &#39;);
<a name="line301"></a>  }
<a name="line302"></a>  if (this.showRelativeTime) {
<a name="line303"></a>    sb.push(&#39;[&#39;, goog.debug.Formatter.getRelativeTime_(logRecord,
<a name="line304"></a>        this.startTimeProvider_.get()), &#39;s] &#39;);
<a name="line305"></a>  }
<a name="line306"></a>
<a name="line307"></a>  if (this.showLoggerName) {
<a name="line308"></a>    sb.push(&#39;[&#39;, logRecord.getLoggerName(), &#39;] &#39;);
<a name="line309"></a>  }
<a name="line310"></a>  if (this.showSeverityLevel) {
<a name="line311"></a>    sb.push(&#39;[&#39;, logRecord.getLevel().name, &#39;] &#39;);
<a name="line312"></a>  }
<a name="line313"></a>  sb.push(logRecord.getMessage(), &#39;\n&#39;);
<a name="line314"></a>  if (this.showExceptionText &amp;&amp; logRecord.getException()) {
<a name="line315"></a>    sb.push(logRecord.getExceptionText(), &#39;\n&#39;);
<a name="line316"></a>  }
<a name="line317"></a>  // If the logger is enabled, open window and write html message to log
<a name="line318"></a>  // otherwise save it
<a name="line319"></a>  return sb.join(&#39;&#39;);
<a name="line320"></a>};
</pre>


</body>
</html>
